package com.example.send.util;

import com.google.common.collect.Range;
import com.google.common.collect.RangeMap;
import com.google.common.collect.TreeRangeMap;

import java.util.*;

public class ZiliaoUtil {

    public static void main(String[] args) throws InterruptedException {
        Map<Double, String> map = new HashMap<>();
        Map<String, Double> invoke = new HashMap<>();

        for (int i = 1; i <= 20; i++) {
            for (int j = 1; j < i; j++) {
                if (i + j > 20) {
                    continue;
                }
                double dij = Math.round(10000.0 * j / i) / 100.0;
                if (!map.containsKey(dij)) {
                    map.put(dij, j + "/" + i);
                    invoke.put(j + "/" + i, dij);
                }
            }
        }

        List<Double> sortD = new ArrayList<>(map.keySet());
        sortD.sort(Comparator.naturalOrder());

        RangeMap<Double, String> rangeMap = TreeRangeMap.create();
        double last = 0;
        double mid = sortD.get(0);
        for (int i = 0; i < sortD.size(); i++) {
            double key = sortD.get(i);
//            System.out.println(key + " " +map.get(key));
            System.out.println(map.get(key));
            if (last == 0) {
                last = key;
                continue;
            }
            double newMid = (key + last) / 2;
            rangeMap.put(Range.closedOpen(mid, newMid), map.get(last));
            last = key;
            mid = newMid;
            if (i == sortD.size() - 1) {
                rangeMap.put(Range.closedOpen(newMid, key), map.get(key));
            }
        }

        Scanner s = new Scanner(System.in);
        while (true) {
            System.out.println("******************************************");
            int randomI = (int) (Math.random() * 9001);
            double randomD = randomI / 100.0;
            System.out.println(randomD);
//            Thread.sleep(3000);
            s.next();
            String str = rangeMap.get(randomD);
            String append = "";
            if (str == null) {
                append += " (";
                String t1Str = rangeMap.get(randomD * 10);
                if (t1Str == null) {
                    String t2Str = rangeMap.get(randomD * 10);
                    if (t2Str != null) {
                        str = t2Str + "00";
                        append += t2Str + " = " + invoke.get(t2Str);
                    }
                } else {
                    str = t1Str + "0";
                    append += t1Str + " = " + invoke.get(t1Str);
                }
                append += ")";
            } else {
                append += " = " + invoke.get(str);
            }
            System.out.println(randomD + " => " + str + append);
            System.out.println("******************************************");
            Thread.sleep(5000);
        }
    }
}
